import { registerMicroApps, start } from "qiankun";
import { createSubApps } from "./config";

const useQianKunHooks = () => {
  const registerApps = (props) => {
    try {
      registerMicroApps(createSubApps(props), {
        beforeLoad: [
          async (app) => {
            console.log("before load", app.name);
          },
        ],
        beforeMount: [
          async (app) => {
            console.log("before mount", app.name);
          },
        ],
        afterMount: [
          async (app) => {
            console.log("after mount", app.name);
          },
        ],
      });
    } catch (e) {
      console.error(e);
    }
  };
  const initQianKun = (props) => {
    if (window.qiankunStarted) return;
    registerApps(props);
    start({
      prefetch: "all",
      sandbox: {
        experimentalStyleIsolation: true, // 样式隔离（非严格模式）
      },
      // 过滤子应用资源加载
      excludeAssetFilter: (assetUrl) => {
        const witeList = ["map.qq.com"];
        return !witeList.some((url) => assetUrl.includes(url));
      },
    });
  };

  return {
    initQianKun,
  };
};

export default useQianKunHooks;
